package com.ruike.alisurface.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.lgh.uvccamera.utils.ThreadUtils;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_http.ApiTools;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.Base64Util;
import com.voodoo.lib_utils.SharedUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class OrcIdUtil {
    static String ocr_Idcard = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";//身份证识别地址

    private static String encodeImage(String path) {
        File imagefile = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(imagefile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Bitmap bm = BitmapFactory.decodeStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String encImage = Base64Util.encode(b);
        //Base64.de
        bm.recycle();
        return encImage;
    }


    public static void getORCIdCard(String fileapth, ApiListener apiListener) {//该接口需要添加addHeader("Authorization", "APPCODE " + "ab3c45977cdb4b1693a58606e60fe51a")
//        String url = "https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
        ThreadUtils.post(new Runnable() {
            @Override
            public void run() {
                String imgs = encodeImage(fileapth);
                JSONObject param = new JSONObject();
                try {
                    param.put("image", imgs);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("side", "face");
                    param.put("configure", jsonObject);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String appcode = SharedUtils.getInstance().getString(ShareKey.APPCODE);
                L.i("身份证配置==addHeader==", appcode);
                ApiTools.getInstance().post(ocr_Idcard, param.toString(), apiListener, appcode);
            }
        });
    }

}
